package com.bitrate.dagger.gateway.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.bitrate.dagger.gateway.xss.DefaultXssCleaner;
import com.bitrate.dagger.gateway.xss.FormXssClean;
import com.bitrate.dagger.gateway.xss.JsonXssClean;
import com.bitrate.dagger.gateway.xss.XssCleaner;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@ConditionalOnWebApplication
@Configuration(proxyBeanMethods = false)
public class XssConfiguration {

	@Bean
	@ConditionalOnMissingBean
	public XssCleaner xssCleaner() {
		return new DefaultXssCleaner();
	}

	@Bean
	public FormXssClean formXssClean(XssCleaner xssCleaner) {
		log.info("-------------Xss Form Cleaner Init Success-----------");
		return new FormXssClean(xssCleaner);
	}

	@Bean
	public Jackson2ObjectMapperBuilderCustomizer xssJacksonCustomizer(XssCleaner xssCleaner) {
		JsonXssClean xssClean = new JsonXssClean(xssCleaner);
		log.info("-------------Xss Json Cleaner Init Success-----------");
		return builder -> builder.deserializerByType(String.class, xssClean);
	}

}
